package com.ufo.store.management.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.widget.MagicIndicator.navigator.ScaleCircleNavigator;
import com.ufo.store.management.widget.viewpager.ViewPagerScroller;
import com.ufo.store.management.widget.viewpager.transformer.StereoscopicPageTransform;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by taoli on 2016/5/13.
 */
public class AdvertisementFragment extends Fragment implements ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private TextView mDotViewTitleTv;
    private ArrayList<String> mDotImageViewTitles = null;
    private AdPagerAdapter mPagerAdapter = null;
    private ViewPagerScroller mViewPagerScroller;
    private MagicIndicator mMagicIndicator = null;
    private AtomicInteger atomicInteger = new AtomicInteger(0);
    private boolean isContinue = true;

    private final Handler viewHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                default:
                    mViewPager.setCurrentItem(msg.what);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewPagerScroller = new ViewPagerScroller(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(
                R.layout.fragment_advertisement, null);
        initView(view);
        return view;
    }

    private void initView(View entryView) {
        mViewPager = (ViewPager) entryView.findViewById(R.id.viewpager);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setPageTransformer(true, new StereoscopicPageTransform());
        mViewPagerScroller.initViewPagerScroll(mViewPager);
        mDotViewTitleTv = (TextView) entryView.findViewById(R.id.title_tv);
        mMagicIndicator = (MagicIndicator) entryView.findViewById(R.id.magic_indicator);
        mPagerAdapter = new AdPagerAdapter();
    }

    private void initMagicIndicator(int size) {
        ScaleCircleNavigator scaleCircleNavigator = new ScaleCircleNavigator(getActivity());
        scaleCircleNavigator.setCircleCount(size);
        scaleCircleNavigator.setNormalCircleColor(getResources().getColor(R.color.app_key_color));
        scaleCircleNavigator.setSelectedCircleColor(getResources().getColor(R.color.app_key_color));
        scaleCircleNavigator.setCircleClickListener(new ScaleCircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {
                mViewPager.setCurrentItem(index);
            }
        });
        mMagicIndicator.setNavigator(scaleCircleNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    public ViewPager getViewPager() {
        return mViewPager;
    }

    public void setData(ArrayList<View> viewLists, ArrayList<String> viewListsTitles) {
        isContinue = false;
        mViewPager.setAdapter(null);
        mPagerAdapter.setViewLists(viewLists);
        mViewPager.setAdapter(mPagerAdapter);

        mDotImageViewTitles = viewListsTitles;
        //mDotViewTitleTv.setText(mDotImageViewTitles.get(0));
        initMagicIndicator(viewLists.size());

        isContinue = true;

        //循环播放
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isContinue) {
                    viewHandler.sendEmptyMessage(atomicInteger.get());
                    atomicOption();
                }
            }
        }).start();
    }

    public class AdPagerAdapter extends PagerAdapter {
        private List<View> mListViews;

        public AdPagerAdapter() {
        }

        public void setViewLists(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mListViews.get(position));//删除页卡
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {  //这个方法用来实例化页卡
            container.addView(mListViews.get(position), 0);//添加页卡
            mListViews.get(position).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (Integer) v.getTag();
                }
            });
            return mListViews.get(position);
        }

        @Override
        public int getCount() {
            return mListViews.size();//返回页卡的数量
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;//官方提示这样写
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int arg0) {
        atomicInteger.getAndSet(arg0);
        //mDotViewTitleTv.setText(mDotImageViewTitles.get(arg0));
    }

    private void atomicOption() {
        atomicInteger.incrementAndGet();
        if (atomicInteger.get() > mPagerAdapter.getCount() - 1) {
            atomicInteger.getAndAdd(-5);
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {

        }
    }
}
